package com.querie.util;

import java.io.BufferedWriter;
import java.io.File;
import java.io.FileWriter;
import java.io.IOException;

import com.querie.config.Configuration;
import com.querie.model.beans.RunInfo;
import com.querie.model.beans.StatInfo;

public class RunDataExcelWriter {

	private int runId;
	private int configurationId;
	private BufferedWriter fileWriter;
	private static BufferedWriter statWriter;
	public RunDataExcelWriter(int configurationId, int runId) {
		super();
		this.configurationId = configurationId;
		this.runId = runId;
		int topNValForQuery =  Configuration.getnValForQuery();
		
		File expDir =  new File("resources/experiments");
		if (!expDir.exists())
		{
			expDir.mkdir();
		}

		File confDir =  new File("resources/experiments/configuration"+configurationId);
		if (!confDir.exists())
		{
			confDir.mkdir();
		}
		
		File excelFile = new File("resources/experiments/configuration"+configurationId+"/run"+runId+".csv");
		if (!excelFile.exists())
		{
			try {
				excelFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		

		try {
			fileWriter = new BufferedWriter(new FileWriter(excelFile));
			fileWriter.write("sessionid,top"+topNValForQuery+"Max Precision,top"+topNValForQuery+"Average Precision,top"+topNValForQuery+"Max Recall,top"+topNValForQuery+"Average Reacall,UnSeenQueryId, RecommendedQueryIdAndTheirPrecisionRecalls");
			fileWriter.newLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}

	}
	public int getRunId() {
		return runId;
	}
	public void setRunId(int runId) {
		this.runId = runId;
	}
	public int getConfigurationId() {
		return configurationId;
	}
	public void setConfigurationId(int configurationId) {
		this.configurationId = configurationId;
	}
	
	public void writeRunInfoToExcel(RunInfo runInfo)
	{
		if (runInfo==null)
		{
			return;
		}
		
		if (fileWriter == null)
		{
			return;
		}
		try {
			fileWriter.write(runInfo.getSessionId()+","+runInfo.getMaxPrecision()+","+runInfo.getAveragePrecision()+","+runInfo.getMaxRecall()+","+runInfo.getAverageRecall()+","+runInfo.getUnSeenQueryId()+","+runInfo.getRecommendedQueryIds());
			fileWriter.newLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	public void insertBlankLines()
	{
		try {
			fileWriter.newLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public void writeCustomOutputToExcel(String input)
	{
		try {
			fileWriter.write(input);
			fileWriter.newLine();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}		
	}
	public void closeAndFlush()
	{
		try {
			fileWriter.flush();
			fileWriter.close();
			fileWriter = null;
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
	public static void main(String a[])
	{
		RunDataExcelWriter excelWriter = new RunDataExcelWriter(Util.getNextConfigurationId(),1);
		RunInfo runInfo =  new RunInfo(1, 0.9, 0.8, 0.7, 0.6, 198, "18:0.8,77:0.9");
		excelWriter.writeRunInfoToExcel(runInfo);
		excelWriter.closeAndFlush();
	}
	
	public static void createConfigurationFile(int configurationId)
	{
		File confFile = new File("resources/experiments/configuration"+configurationId+"/Configuration.csv");
		if (!confFile.exists())
		{
			try {
				confFile.createNewFile();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}
		}
		try {
			BufferedWriter writer = new BufferedWriter(new FileWriter(confFile));
			writer.write(Configuration.getConfigurationString());
			writer.flush();
			writer.close();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
	}
	public static void writeStatistics(int configurationId, StatInfo statInfo)
	{
		File statsFile = new File("resources/experiments/configuration"+configurationId+"/statistics.csv");
		if (!statsFile.exists())
		{
			try {
				statsFile.createNewFile();
				
				statWriter = new BufferedWriter(new FileWriter(statsFile));
				statWriter.write("Run, Average of Max Precision, Average of Avg Precision, Average of Max Recall, Average of Avg Recall");
				statWriter.newLine();
				statWriter.flush();
			} catch (IOException e) {
				// TODO Auto-generated catch block
				e.printStackTrace();
			}			
		}
		try {
			statWriter.write(statInfo.getRunInfo()+","+statInfo.getAvgOfMaxPrecisions()+","+statInfo.getAvgOfAvgPrecisions()+","+statInfo.getAvgOfMaxRecall()+","+statInfo.getAvgOfAvgRecall()+"\n");
			statWriter.newLine();
			statWriter.flush();
		} catch (IOException e) {
			// TODO Auto-generated catch block
			e.printStackTrace();
		}
		
	}
	
	public static  void closeAndFlushStatWriter()
	{
		try {
			statWriter.flush();
			statWriter.close();
		} catch (IOException e) {
			e.printStackTrace();
		}
	}
	
}
